﻿package edzis.translatebuddy.view.components {
	import edzis.translatebuddy.events.DataProxyEvent;
	import edzis.translatebuddy.model.vo.WordVO;
	import fl.data.DataProvider;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	/**
	 * The View Component for adding a word to the translation list
	 * @author Edgars Simsons
	 */
	public class AddWord extends AddWord_design {
		private var _enterDown	:Boolean;
		
		public function AddWord() {
			visible = false;
		//	wordTxt.autoFillEnabled = true;
		}
		
		public function init(wordsSession:DataProvider) :void {
			ArthropodDebug.log("init");
		//	wordTxt.dataProvider = wordsSession;
		//	wordTxt.addEventListener(Event.CHANGE, onWordChange);
			wordTxt.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			wordTxt.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			addBtn.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			addBtn.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			addBtn.addEventListener(MouseEvent.CLICK, onAddWord);
			
		//	wordTxt.dataProvider = new DataProvider([ { label:"aas" }, { label:"aaz"}, { label:"azs" }, { label:"bas" } ]);
		}
		
		private function onWordChange(e:Event):void {
			ArthropodDebug.log("onWordChange " + e.target);
			ArthropodDebug.log("onWordChange " + e.toString());
		//	typedWord = e.ta
		}
		
		private function onAddWord(e:MouseEvent):void {
		//	ArthropodDebug.log("wordTxt.selectedLabel" + wordTxt.selectedLabel);
			if (wordTxt.text != "") {
			ArthropodDebug.log("onAddWord " + wordTxt.text);
				var newWord:WordVO = new WordVO( wordTxt.text, translationTxt.text );
				dispatchEvent( new DataProxyEvent(DataProxyEvent.ADD_WORD, newWord));
				wordTxt.text = "";
				translationTxt.text = "";
				stage.focus = wordTxt;
			}
		}
		
		private function onKeyDown(e:KeyboardEvent):void {
			if (e.keyCode == Keyboard.ENTER && !_enterDown) {
				_enterDown = true;
				onAddWord(null);
			}
		}
		
		private function onKeyUp(e:KeyboardEvent):void {
			if (e.keyCode == Keyboard.ENTER) _enterDown = false;
		}
		
		private function onWordUpdate(e:Event):void{
		}
		
		private function onTranslationUpdate(e:Event):void{
			
		}
		
	}
	
}